#!/bin/bash

# SPAM3UK 本地数据抓取脚本

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
PURPLE='\033[0;35m'
NC='\033[0m' # No Color

# 本地服务器配置
LOCAL_HOST="localhost"
LOCAL_PORT="8080"
BASE_URL="http://${LOCAL_HOST}:${LOCAL_PORT}"

echo "🚀 SPAM3UK 本地数据抓取"
echo "=================================================="
echo "本地服务器: ${BASE_URL}"
echo "开始时间: $(date)"
echo ""

# 检查服务器是否正常运行
echo "1️⃣ 检查服务器状态"
echo "----------------------------------------"
health_status=$(curl -s "${BASE_URL}/health" | jq -r '.status' 2>/dev/null)
if [ "$health_status" = "ok" ]; then
    echo -e "${GREEN}✅ 服务器运行正常${NC}"
else
    echo -e "${RED}❌ 服务器未正常响应，请确认服务已启动${NC}"
    exit 1
fi

# 查看当前数据状态
echo ""
echo "2️⃣ 查看当前数据状态"
echo "----------------------------------------"
vehicle_count=$(curl -s "${BASE_URL}/api/vehicles" | jq 'length' 2>/dev/null)
echo "当前车辆数据: $vehicle_count 条记录"

trim_focus=$(curl -s "${BASE_URL}/api/get_trims?model=Focus" 2>/dev/null | jq -r '.total_trims' 2>/dev/null)
echo "Focus车型配置: $trim_focus 个"

# 执行全量数据抓取
echo ""
echo "3️⃣ 执行全量数据抓取"
echo "----------------------------------------"
echo -e "${YELLOW}⚠️ 开始执行全量抓取，这可能需要几分钟时间...${NC}"
echo "请求URL: ${BASE_URL}/api/scrape_all"

start_time=$(date +%s)
echo ""
echo "📥 开始抓取..."

# 执行抓取请求，设置较长的超时时间
response=$(curl -s -w "%{http_code}" -o /tmp/scrape_response.json --max-time 300 -X POST "${BASE_URL}/api/scrape_all" 2>/dev/null)
status_code="${response: -3}"
end_time=$(date +%s)
duration=$((end_time - start_time))

echo ""
echo "📊 抓取结果:"
echo "----------------------------------------"
echo "HTTP状态码: $status_code"
echo "耗时: $duration 秒"

if [ "$status_code" = "200" ]; then
    echo -e "${GREEN}✅ 抓取成功！${NC}"
    
    # 显示抓取结果摘要
    if [ -f /tmp/scrape_response.json ]; then
        echo ""
        echo "📋 抓取结果详情:"
        cat /tmp/scrape_response.json | jq '.' 2>/dev/null || cat /tmp/scrape_response.json
    fi
else
    echo -e "${RED}❌ 抓取失败${NC}"
    if [ -f /tmp/scrape_response.json ]; then
        echo "错误详情:"
        cat /tmp/scrape_response.json
    fi
fi

# 验证抓取后的数据状态
echo ""
echo "4️⃣ 验证抓取后数据状态"
echo "----------------------------------------"

new_vehicle_count=$(curl -s "${BASE_URL}/api/vehicles" | jq 'length' 2>/dev/null)
echo "抓取后车辆数据: $new_vehicle_count 条记录"

if [ "$new_vehicle_count" -gt "$vehicle_count" ]; then
    added_count=$((new_vehicle_count - vehicle_count))
    echo -e "${GREEN}✅ 新增了 $added_count 条车辆记录${NC}"
else
    echo -e "${YELLOW}⚠️ 车辆数据未增加，可能是重复数据或抓取失败${NC}"
fi

# 检查特定车型的Trim配置
echo ""
echo "5️⃣ 检查重点车型Trim配置"
echo "----------------------------------------"

models=("Focus" "Fiesta" "Puma" "Kuga" "Mustang" "Transit")
for model in "${models[@]}"; do
    echo -n "• $model: "
    trim_result=$(curl -s "${BASE_URL}/api/get_trims?model=$model" 2>/dev/null)
    status=$(echo "$trim_result" | jq -r '.message' 2>/dev/null)
    total_trims=$(echo "$trim_result" | jq -r '.total_trims' 2>/dev/null)
    
    if [ "$status" = "Success" ] && [ "$total_trims" != "null" ] && [ "$total_trims" -gt 0 ]; then
        echo -e "${GREEN}✅ $total_trims 个配置${NC}"
    else
        echo -e "${RED}❌ 暂无配置数据${NC}"
    fi
done

# 尝试抓取特定车型的Trim数据
echo ""
echo "6️⃣ 补充抓取重点车型Trim配置"
echo "----------------------------------------"

priority_models=("Puma" "Mustang" "Kuga")
for model in "${priority_models[@]}"; do
    echo "正在抓取 $model 车型配置..."
    trim_response=$(curl -s -w "%{http_code}" -o /tmp/trim_scrape.json -X POST "${BASE_URL}/api/scrape_trims?model=$model" 2>/dev/null)
    trim_status="${trim_response: -3}"
    
    if [ "$trim_status" = "200" ]; then
        echo -e "  ${GREEN}✅ $model 配置抓取成功${NC}"
        
        # 验证抓取结果
        verification=$(curl -s "${BASE_URL}/api/get_trims?model=$model" 2>/dev/null)
        trim_count=$(echo "$verification" | jq -r '.total_trims' 2>/dev/null)
        if [ "$trim_count" != "null" ] && [ "$trim_count" -gt 0 ]; then
            echo -e "  📊 找到 $trim_count 个配置"
        fi
    else
        echo -e "  ${RED}❌ $model 配置抓取失败 (HTTP $trim_status)${NC}"
        if [ -f /tmp/trim_scrape.json ]; then
            error_msg=$(cat /tmp/trim_scrape.json | jq -r '.message' 2>/dev/null || cat /tmp/trim_scrape.json)
            echo -e "  🚨 错误: $error_msg"
        fi
    fi
    echo ""
done

# 最终数据统计
echo "7️⃣ 最终数据统计"
echo "----------------------------------------"
final_vehicle_count=$(curl -s "${BASE_URL}/api/vehicles" | jq 'length' 2>/dev/null)
categories=$(curl -s "${BASE_URL}/api/categories" | jq 'length' 2>/dev/null)

echo "📊 数据概览:"
echo "• 车辆总数: $final_vehicle_count"
echo "• 分类总数: $categories"

# 按分类统计
echo ""
echo "📋 分类统计:"
categories_data=$(curl -s "${BASE_URL}/api/categories" 2>/dev/null)
echo "$categories_data" | jq -r '.[] | "• \(.category): \(.count) 个车辆"' 2>/dev/null

echo ""
echo "🎯 快速验证链接:"
echo "----------------------------------------"
echo "• 健康检查: ${BASE_URL}/health"
echo "• 所有车辆: ${BASE_URL}/api/vehicles"
echo "• Web界面: ${BASE_URL}/web/"
echo "• Focus配置: ${BASE_URL}/api/get_trims?model=Focus"
echo "• 分类统计: ${BASE_URL}/api/categories"

echo ""
echo "✨ 抓取任务完成"
echo "=================================================="
echo "完成时间: $(date)"
echo "总耗时: $(($(date +%s) - start_time)) 秒"

# 清理临时文件
rm -f /tmp/scrape_response.json /tmp/trim_scrape.json

echo ""
echo -e "${CYAN}🎉 本地服务器数据已更新，可以开始使用API服务！${NC}"
